"""
# Author : YuuSoo
# version : python 3.8
# Time : 2023/1/6 22:16
"""
# -*- coding: utf-8 -*-
# =============================================================================
# import....
import logging
from datetime import datetime, date

import flask_restful
from flask_restful import Resource, reqparse
import numpy as np
import pandas as pd
import time
import utils
import json

# 自定义模块
from errors import my_abort, ResponseCode

# 自定义错误
flask_restful.abort = my_abort
root_logger = logging.getLogger('root')
logger = logging.getLogger('main')
# =============================================================================

# =============================================================================
# 模型import
from .Yjprocess_Recommendations import function


# =============================================================================


class YJTJ_Process_Recommendations(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser(bundle_errors=True)
        # 根据参数需求增删
        # printOrWeld, PCBThickness, PCBLength, PCBWidth, Special_Dev, RecommendationCount
        self.parser.add_argument('PCBThickness', type=str, required=True, help="PCB板厚")
        self.parser.add_argument('PCBLength', type=str, required=True, help="PCB板长")
        self.parser.add_argument('PCBWidth', type=str, required=True, help="PCB板宽")
        self.parser.add_argument('Special_Dev', type=str, required=True, help="A有无特殊温度要求器件")
        self.parser.add_argument('BGA_status_A', type=str, required=True, help="A有无BGA")
        self.parser.add_argument('Temperature_A', type=str, required=True, help="A特殊温度")
        self.parser.add_argument('Special_Dev_B', type=str, required=True, help="B有无特殊温度要求器件")
        self.parser.add_argument('BGA_status_B', type=str, required=True, help="B有无BGA")
        self.parser.add_argument('Temperature_B', type=str, required=True, help="B特殊温度")
        self.parser.add_argument('Company', type=str, required=True, help="公司")
        self.parser.add_argument('Temperature_field_nums', type=str, required=True, help="温区数量")

        # self.parser.add_argument('RecommendationCount', type=str, required=True, help="推荐的数量")

    # def get(self):
    #     currentTimeStr = utils.nowTimeText4China()
    #     print('=== 工艺参数推荐开始, 中国时间: %s' % currentTimeStr)
    #
    #     computeDurationStart = time.time()
    #     httpStatusCode = 200
    #     queryParams = self.parser.parse_args()
    #     # 获取参数，根据参数需求增删
    #     printOrWeld = queryParams.get('printOrWeld')
    #     PCBThickness = queryParams.get('PCBThickness')
    #     PCBLength = queryParams.get('PCBLength')
    #     PCBWidth = queryParams.get('PCBWidth')
    #     Special_Dev = queryParams.get('Special_Dev')
    #     RecommendationCount = queryParams.get('RecommendationCount')
    #
    #     logger.info(
    #         '=== 工艺参数推荐请求 ==> GET ?\nprintOrWeld=%s\nPCBThickness=%s\nPCBLength=%s\nPCBWidth=%s\nSpecial_Dev=%s\nRecommendationCount=%s' % (
    #         printOrWeld, PCBThickness, PCBLength, PCBWidth, Special_Dev, RecommendationCount))
    #     res = function(printOrWeld, PCBThickness, PCBLength, PCBWidth, Special_Dev, RecommendationCount)
    #
    #     if res['resp_code'] == ResponseCode.WRONG_PARAM:
    #         httpStatusCode = 400
    #     computeDuration = time.time() - computeDurationStart
    #     logger.info('=== 工艺参数推荐 ==> 耗时( %.3f 秒 )' % computeDuration)
    #     return res, httpStatusCode, {"Content-Type": "application/json; charset=utf-8"}

    def post(self):
        currentTimeStr = utils.nowTimeText4China()
        print('=== 工艺参数推荐开始, 中国时间: %s' % currentTimeStr)

        computeDurationStart = time.time()
        httpStatusCode = 200
        queryParams = self.parser.parse_args()
        # 获取参数，根据参数需求增删
        PCBThickness = queryParams.get('PCBThickness')
        PCBLength = queryParams.get('PCBLength')
        PCBWidth = queryParams.get('PCBWidth')
        Special_Dev = queryParams.get('Special_Dev')
        Company = queryParams.get('Company')
        BGA_status_A = queryParams.get('BGA_status_A')
        Temperature_A = queryParams.get('Temperature_A')
        Special_Dev_B = queryParams.get('Special_Dev_B')
        BGA_status_B = queryParams.get('BGA_status_B')
        Temperature_B = queryParams.get('Temperature_B')

        Temperature_field_nums = queryParams.get('Temperature_field_nums')
        # RecommendationCount = queryParams.get('RecommendationCount')

        logger.info(
            '=== 工艺参数推荐请求 ==> POST ?\nPCBThickness=%s\nPCBLength=%s\nPCBWidth=%s\nSpecial_Dev=%s\nCompany=%s\nBGA_status_A=%s\nTemperature_A=%s\nSpecial_Dev_B=%s\nBGA_status_B=%s\nTemperature_B=%s\nTemperature_field_nums=%s' % (
                PCBThickness, PCBLength, PCBWidth, Special_Dev,Company,BGA_status_A,Temperature_A,Special_Dev_B,BGA_status_B,Temperature_B,Temperature_field_nums))
        res = function(PCBThickness, PCBLength, PCBWidth, Special_Dev,Company,BGA_status_A,Temperature_A,Special_Dev_B,BGA_status_B,Temperature_B,Temperature_field_nums)

        if res['resp_code'] == ResponseCode.WRONG_PARAM:
            httpStatusCode = 400
        computeDuration = time.time() - computeDurationStart
        logger.info('=== 工艺参数推荐 ==> 耗时( %.3f 秒 )' % computeDuration)
        return res, httpStatusCode, {"Content-Type": "application/json; charset=utf-8"}
        # return {"message": "post, DjdpFeature!"}


# class NpEncoder(json.JSONEncoder):  # 继承json.JSONEncoder
#     def default(self, obj):
#         if isinstance(obj, np.integer):
#             return int(obj)
#         elif isinstance(obj, np.floating):
#             return float(obj)
#         elif isinstance(obj, np.float):
#             return float(obj)
#         elif isinstance(obj, np.ndarray):
#             return obj.tolist()
#         elif isinstance(obj, pd.Timestamp):
#             return obj.strftime('%Y-%m-%d %H:%M:%S')
#         elif isinstance(obj, datetime):
#             return obj.strftime('%Y-%m-%d %H:%M:%S')
#         elif isinstance(obj, date):
#             return obj.strftime('%Y-%m-%d')
#         elif isinstance(obj, np.datetime64):
#             return np.datetime_as_string(obj)
#         else:
#             return super(NpEncoder, self).default(obj)
